package helper

import (
	"crypto/md5"
	"fmt"
	"math/rand"
	"migrate/common"
)

func ElementExsit[T string | common.Int | common.Uint | common.Float](slice []T, element T) bool {
	for _, v := range slice {
		if element == v {
			return true
		}
	}
	return false
}

func Diff(slice1, slice2 []string) (result []string) {
	var tmp = make(map[string]string)

	if (len(slice1) < 1) && len(slice1) < 1 {
		return
	}

	for _, v := range slice1 {
		tmp[v] = v
	}
	for _, v := range slice2 {
		if tmp[v] != "" {
			delete(tmp, v)
		} else {
			tmp[v] = v
		}
	}
	for _, v := range tmp {
		result = append(result, v)
	}

	return
}

func GenerateRandomString(n int) string {
	letters := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}

	return string(b)
}

func Md5(str string) string {
	return fmt.Sprintf("%x", md5.Sum([]byte(str)))
}
